* Scientists Running for Office
* matthew.motta@appc.upenn.edu

clear
cd "WHERE YOU KEEP THE DATA"
insheet using "THE DATA", names
cd "WHERE YOU WANT RESULTS TO GO"


**********************************
* Figure: Candidate Descriptives
**********************************

* PARTISANSHIP

clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"

drop if degree_b==.	

graph bar (mean) democrat gop third  if race=="house" | race=="senate",  scheme(s1mono) ///
	bar(1, lcolor(blue) fcolor(blue) fintensity(30)) ///
	bar(2, lcolor(red) fcolor(red)  fintensity(30)) ///
	bar(3, lcolor(yellow) fcolor(yellow)  fintensity(30)) ///
	blabel(bar, size(small) format(%9.2f)) ///
	ytitle("% of Candidates" "with STEM Backgrounds" " ") ///
	legend(ring(0) pos(1) label(1 "Democrats") label(2 "Republicans") label(3 "Third Party") col(1) size(vsmall) symxsize(vsmall) region(lcolor(none) fcolor(none))) ///
	ylab(0 .25 "25" 0.50 "50" 0.75 "75" 1 "100")  ///
	title("(a)" " ")
	
	graph save "party", replace

* GENDER

clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"

drop if degree_b==.	


graph bar (mean) female male  if race=="house" | race=="senate",  scheme(s1mono) ///
	bar(1, lcolor(pink) fcolor(pink) fintensity(30)) ///
	bar(2, lcolor(blue) fcolor(blue) fintensity(30)) ///
	blabel(bar, size(small) format(%9.2f)) ///
	ytitle("% of Candidates" "with STEM Backgrounds" " ") ///
	legend(ring(0) pos(11) label(1 "Women") label(2 "Men") col(1) ///
		symxsize(vsmall) size(vsmall) region(lcolor(none) fcolor(none))) ///
	ylab(0 .25 "25" 0.50 "50" 0.75 "75" 1 "100") ///
	title("(d)" " ")
	
	graph save "gender", replace
	
	
* Degree

	gen bachelor=.
		replace bachelor=1 if degree_b==1
		replace bachelor=0 if degree_b==2 | degree_b==3
		fre bachelor
		
	gen master=.
		replace master=1 if degree_b==2
		replace master=0 if degree_b==1 | degree_b==3
		fre master
		
	gen doctor=.
		replace doctor=1 if degree_b==3
		replace doctor=0 if degree_b==1 | degree_b==2
		fre doctor

graph bar (mean) bachelor master doctor if race=="house" | race=="senate", ///
	 scheme(s1mono) ///
	bar(1, lcolor(black) fcolor(blue) fintensity(20)) ///
	bar(2, lcolor(black) fcolor(blue) fintensity(50)) ///
	bar(3, lcolor(black) fcolor(blue) fintensity(80)) ///
	blabel(bar, size(small) format(%9.2f)) ///
	legend(label(1 "Bachelors") label(2 "Masters") label(3 "Doctorate") ///
		ring(0) pos(11) col(1) size(vsmall) symxsize(vsmall) region(lcolor(none) fcolor(none))) ///
	ytitle("% of Candidates" "with STEM Backgrounds" " ") ///
	ylab(0 .1 "10" .2 "20" .3 "30" .4 "40" .5 "50") ///
	title("(b)" " ")
		
		graph save "degree", replace
		
* Electoral Status
	
clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"	
	
drop if degree_b==.	
	
	
	gen x=1

	gen won=.
		replace won=1 if status=="win" // Won whole thing
		replace won=0 if status!="win" // Won whole thing
		
	gen wplg=.  // Won the primary, but lost the general
		replace wplg=1 if status=="general loss"
		replace wplg=0 if status!="general loss"
		
	gen lostp=. // Lost primary
		replace lostp=1 if status=="primary loss"
		replace lostp=0 if status!="primary loss"
	
	gen dw=. // Didn't qualify for primary
		replace dw=1 if status=="withdrew" | status=="disqualified"
		replace dw=0 if status!="withdrew" & status!="disqualified"

	graph bar (mean) dw lostp wplg won  if race=="house" | race=="senate", ///
		 scheme(s1mono) ///
		bar(1, lcolor(black) fcolor(red) fintensity(20)) ///
		bar(2, lcolor(black) fcolor(red) fintensity(40)) ///
		bar(3, lcolor(black) fcolor(red) fintensity(60)) ///
		bar(4, lcolor(black) fcolor(red) fintensity(90)) ///
		blabel(bar, size(small) format(%9.2f)) ///
		legend(label(1 "Withdrew") label(2 "L. Primary") ///
			label(3 "L. General") label(4 "Won") ///
			ring(0) pos(11) col(1) size(tiny) symxsize(tiny) col(.1) region(lcolor(none) fcolor(none))) ///
		ytitle("% of Candidates" "with STEM Backgrounds" " ") ///
		ylab(0  .2 "20"  .4 "40" .6 "60") ///
		title("(e)" " ")
		
		graph save "results", replace
		
	
* First Time	

clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"	

drop if degree_b==.	

gen never=0
	replace never=1 if firsttime=="yes"

gen local=0
	replace local=1 if firsttime=="local"
	
gen ranbefore=0
	replace ranbefore=1 if firsttime=="no"

graph bar (mean) never ranbefore local if race=="house" | race=="senate" & party=="democrat", ///
	 scheme(s1mono) ///
	bar(1, lcolor(black) fcolor(purple) fintensity(20)) ///
	bar(2, lcolor(black) fcolor(purple) fintensity(50)) ///
	bar(3, lcolor(black) fcolor(purple) fintensity(80)) ///
	blabel(bar, size(small) format(%9.2f)) ///
	legend(label(1 "First Time") label(2 "Congress") label(3 "Local Office") ///
		ring(0) pos(1) col(1) size(vsmall) symxsize(vsmall) region(lcolor(none) fcolor(none))) ///
	ytitle("% of DEMOCRATIC Candidates" "with STEM Backgrounds" " ") ///
	ylab(0  .2 "20" .4 "40" .6 "60" .8 "80") ///
	title("(f)" " ") 
	
	graph save "ranbefore", replace
		
	
* What fields are most represented?

clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"

drop if degree_b==.	


keep if race=="house" | race=="senate"

gen x=1/157

collapse (sum) x, by(field_broad)
	
	* A few recodes
	
	replace field_broad="geoscience" if field_broad=="geology"
	replace field_broad="biology" if field_broad=="biophysics" | field_broad=="biochemistry"
	
graph bar x, over(field, sort(1) label(nolabels)) ///
	ytitle("% of Candidates" "with STEM Backgrounds" " ") ///
	ylab(0 .10 "10" .20 "20" .30 "30" .40 "40") scheme(s1mono) ///
	bar(1, lcolor(green) fcolor(green) fintensity(50)) ///
	blabel(group, size(vsmall) angle(-45) orientation(vertical)) ///
	title("(c)" " ")
	
	
	graph save "field", replace
	
	
*** Combined

graph combine "party" "degree" "field" "gender" "results" "ranbefore", col(3) scheme(s1mono)
	* will add to this electoral performance AND endorsement by party
	* remember to label (a), (b), etc
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence/PS Submission"	
	graph export "f1.pdf", replace



***********************************
* TABLE: Model Advance from Primary
***********************************


** Just the House, and without Incumbents

gen trump01=(trump-5.4)/(76.7-5.4)
gen educ01=(education-0.09)/(0.62-0.09)

encode statdist, gen(statdist_n)
encode state, gen(state_n)	
	
	
* Table 1 with JUST Democrats

logit adv endorse never female trump01 if incumb==0 & dem==1, vce(cluster statdist_n)
	est store adv_dem1

	margins, at(endorse=0 endorse=1)

		
logit adv endorse never female educ01 if incumb==0 & dem==1, vce(cluster statdist_n)
	est store adv_dem2
	
	margins, at(endorse=0 endorse=1)

	
logit adv endorse never female trump01 if incumb==0 & dem==1, vce(cluster state_n)
	est store adv_dem3
	
	margins, at(endorse=0 endorse=1)

	
logit adv endorse never female educ01 if incumb==0 & dem==1, vce(cluster state_n)
	est store adv_dem4

	margins, at(endorse=0 endorse=1)

	
	
	estout adv_d*, ///
	cells(b (star fmt(2)  vacant("-")) se(par(( )))) ///
         starlevels(+ 0.10 * 0.05) stats(N r2, fmt(0 2)) ///
		 varlabels(endorse314 "Contribution from 314 PAC" ///
			never "First Time Candidate" female "Female (vs. Male)" ///
			trump01 "District Trump Vote Share" educ01 "% College Educ. in District" ///
			gop "Republican" third "Third Party") ///
		 order(endorse314 gop third never female trump01 educ01) style(tex)

	
		
**************************
* Summary Stats
**************************

clear
cd "/Users/mmotta/Google Drive/APPC/Science Not Silence"
use "runningforscience_postelection"

	drop if degree_b==.

	gen bachelor=.
		replace bachelor=1 if degree_b==1
		replace bachelor=0 if degree_b==2 | degree_b==3
		fre bachelor
		label values bachelor yn
		label variable bachelor "Bachelors Degree"
		
	gen master=.
		replace master=1 if degree_b==2
		replace master=0 if degree_b==1 | degree_b==3
		fre master
		label values master yn
		label variable master "Masters Degree"
		
	gen doctor=.
		replace doctor=1 if degree_b==3
		replace doctor=0 if degree_b==1 | degree_b==2
		fre doctor
		label values doctor yn
		label variable doctor "Doctorate"
		
	gen adv=.  // Won the primary, but lost the general
		replace adv=1 if status=="general loss" | status=="win"
		replace adv=0 if status!="general loss" & status!="win"
		label variable adv "Advanced Beyond Primary"
		label values adv yn
		
	gen lostp=. // Lost primary
		replace lostp=1 if status=="primary loss"
		replace lostp=0 if status!="primary loss"
		label variable lostp "Lost Primary"
		label values lostp yn

	
	gen dw=. // Didn't qualify for primary
		replace dw=1 if status=="withdrew" | status=="disqualified"
		replace dw=0 if status!="withdrew" & status!="disqualified"
		label variable dw "Didn't Qualify / Withdrew"
		label values dw yn
		
	gen won=.
		replace won=1 if status=="win"
		replace won=0 if status!="win"
		label variable won "Won General"
		label values won yn
		

	gen never=0
		replace never=1 if firsttime=="yes"
		label variable never "Never Ran for Congress Before"
		label values never yn

	gen local=0
		replace local=1 if firsttime=="local"
		label variable local "Ran for Local, but not Cong."
		label values local yn

	gen ranbefore=0
		replace ranbefore=1 if firsttime=="no"
		label variable ranbefore "Ran for Congress Before"
		label values ranbefore yn

	gen advdeg=0
		replace advdeg=1 if master==1 | doctor==1
		label variable advdeg "MS or PhD in STEM"
		label values advdeg yn
		fre advdeg

sutex if race=="house" | race=="senate", minmax 
	
	
	
